<?php
// +----------------------------------------------------------------------
// | SparkShop 坚持做优秀的商城系统
// +----------------------------------------------------------------------
// | Copyright (c) 2022~2099 http://sparkshop.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai  <1902822973@qq.com>
// +----------------------------------------------------------------------

namespace app\index\controller;

use app\service\CardRechargeService;
use app\model\card\Card as CardModel;
use app\model\card\CardLog as CardLogModel;
use app\service\CipherService;

class Card extends Base
{
    public function initialize()
    {
        parent::initialize();
        pcLoginCheck();
    }

    /**
     * 余额充值
     */
    public function recharge(CardRechargeService $cardRechargeService)
    {
        $param = input('post.');
        $param['platform'] = '';
        if($param['usamount'] > 0){}else{ 
            $return = array('code'=>1,'msg'=>'充值金额不能小于0');
            return json($return);
        }

        return json($cardRechargeService->createOrder($param, 2));
    }

    /**
     * 检测订单状态
     */
    public function checkOrderStatus(CardRechargeService $cardRechargeService)
    {
        return json($cardRechargeService->checkOrderStatus(input('param.order')));
    }
    /**
     * 卡号列表
     */
    public function mylist(CardModel $cardModel)
    {
        $param = input('param.');
        $userInfo = getFrontUserInfo();
        $where = "user_id = ".$userInfo['id']." and is_del = 0";
        if($param['cate_id'] > 0){
            $where.=" and cate_id = ".$param['cate_id']; 
        }
        if(isset($param['keys']) && $param['keys']){
            $where.=" and sp_card.keys like '%".$param['keys']."%'"; 
        }
        $list = $cardModel->with('cate')->where($where)->order('id desc')->paginate($param['limit']);
        $return = array('code'=>0,'msg'=>'success','data'=>$list);
        return json($return);
    }
    /**
     * 流水列表
     */
    public function log(CardModel $cardModel,CardLogModel $cardLogModel)
    {
        $param = input('param.');
        $userInfo = getFrontUserInfo();
        $searchwhere = "";
        if($param['cate_id'] > 0){
            $searchwhere =" and cate_id = ".$param['cate_id']; 
        }
        $cardIds = $cardModel->where("user_id = ".$userInfo['id']." and is_del = 0".$searchwhere)->column('id');
        $where = "card_id in (".implode(',',$cardIds).") and is_del = 0";
        
        if(isset($param['keys']) && $param['keys']){
            $where.=" and sp_card_log.keys like '%".$param['keys']."%'"; 
        }
        $list = $cardLogModel->with('cate')->where($where)->order('id desc')->paginate($param['limit']);
        $return = array('code'=>0,'msg'=>'success','data'=>$list);
        return json($return);
    }
    /**
     * 卡密重置
     */
    public function reset(CardModel $cardModel,CardLogModel $cardLogModel,CipherService $cipherService)
    {
        $param = input('param.');
        $userInfo = getFrontUserInfo();
        $times = date('Y-m-d H:i:s');
        if(isset($param['id']) && $param['id'] > 0){
            $card = $cardModel->where("user_id = ".$userInfo['id']." and is_del = 0 and id = ".$param['id'])->find();
            $newArr = $cipherService->key_generate(1,0);
            $new_keys = "sk".$card['cate_id']."-".$newArr[0];

            if($cardLogModel->insertGetId(['type_id'=>3,'type2_id'=>0,'card_id'=>$card['id'],'keys'=>$card['keys'],'new_keys'=>$new_keys,'create_time'=>$times])){
                $cardModel->where("id = ".$card['id'])->update(['keys'=>$new_keys,'use_device_nums'=>0]);
                $return = array('code'=>0,'msg'=>'success','data'=>$card);
                return json($return);
            }else{
                $return = array('code'=>1,'msg'=>'重置失败','data'=>[]);
                return json($return);
            }
        }else{
            $return = array('code'=>1,'msg'=>'缺少参数','data'=>[]);
            return json($return);
        }
    }
}